package cn.com.surker.workcon.service.bcs;

import cn.com.surker.base.system.dao.SysStorageLogDao;
import cn.com.surker.base.system.entity.SysLine;
import cn.com.surker.base.system.entity.SysStorageLog;
import cn.com.surker.enums.StateEnum;
import cn.com.surker.lab.entity.LabExpendOther;
import cn.com.surker.util.DateUtil;
import cn.com.surker.workcon.common.GKJDBCService;
import cn.com.surker.workcon.service.ManualService;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 手动配料 Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2024-09-26
 */
public class ManualServiceImpl extends GKJDBCService implements ManualService {

	@Resource
	private SysStorageLogDao sysStorageLogDao;

	@Override
	public List<LabExpendOther> findList(SysLine line, Date startTime) {
		if (line == null) {
			return null;
		}
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return null;
		}
		//	查询sql
		String sql = "select DatTim,Storage,Material,Amount,Operator,Flag,Station from Manual where DatTim > ? and Station = ?";
		Object[] params = {startTime, line.getControlLineId()};
		List<Map<String, Object>> bcsDataList = lineJdbcTemp.queryForList(sql, params);
		if (bcsDataList == null || bcsDataList.isEmpty()) {
			return null;
		}
		Long lineId = line.getId();
		//	转换数据
		List<LabExpendOther> labExpendOtherList = new ArrayList<>();
		for (int i = 0, s = bcsDataList.size(); i < s; i++) {
			Map<String, Object> bcsData = bcsDataList.get(i);
			//	工控料仓名
			String bcsStorage = String.valueOf(bcsData.get("Storage"));
			//	工控材料名
			String bcsMaterial = String.valueOf(bcsData.get("Material"));
			//	下料时间
			Date datTim = DateUtil.convertDate(String.valueOf(bcsData.get("DatTim")));
			String taskTime = DateUtil.convertString(datTim);
			//	操作工
			String operator = String.valueOf(bcsData.get("Operator"));
			//	操作数量
			Double amount = Double.valueOf(String.valueOf(bcsData.get("Amount")));
			//	根据工控数据,查询erp的仓位
			//	erp的原料id
			Long erpMaterialId = null;
			//	工控机的原料id
			Long erpStorageId = null;
			SysStorageLog takeTimeOrStorage = sysStorageLogDao.findTakeTimeOrStorage(lineId, bcsMaterial, 1, taskTime);
			if (takeTimeOrStorage != null) {
				String erpMaterial = takeTimeOrStorage.getMaterial();
				erpMaterialId = takeTimeOrStorage.getMaterialId();
				erpStorageId = takeTimeOrStorage.getStorageId();
			}
			//	创建erp手动消耗数据
			LabExpendOther labExpendOther = new LabExpendOther();
			labExpendOther.setLineId(lineId);
			labExpendOther.setMaterialId(erpMaterialId);
			labExpendOther.setStorageId(erpStorageId);
			labExpendOther.setDosage(amount);
			labExpendOther.setStatus(StateEnum.ENABLED.getCode());
			labExpendOther.setRemark(operator);
			labExpendOther.setCreator(bcsCreater);
			labExpendOther.setExpendTime(datTim);
			labExpendOther.setCreateTime(new Date());
			labExpendOther.setControlMaterial(bcsMaterial);
			labExpendOther.setControlStorage(bcsStorage);
			labExpendOtherList.add(labExpendOther);
		}
		return labExpendOtherList;
	}

	@Override
	public void updateFlag(SysLine line, Date startTime) {
		if (line == null) {
			return;
		}
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return;
		}
		//	更新sql
		String updateSql = "update Manual set flag = '1y' where flag = '1' and Station = ?";
		Object[] params = {line.getControlLineId()};
		//	开启事务
		Long lineId = line.getId();
		boolean b = beginTransaction(lineId);
		//	更新数据
		lineJdbcTemp.update(updateSql, params);
	}

	@Override
	public List<LabExpendOther> findAllList(Long lineId, String startTime, String endTime) {
		SysLine line = getLine(lineId);
		if (line == null) {
			return null;
		}
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return null;
		}
		Long controlLineId = line.getControlLineId();
		//	查询sql
		String sql = "select DatTim,Storage,Material,Amount,Operator,Flag,Station from Manual where DatTim >= ? and DatTim < ? and Station = ?";
		Object[] params = {startTime, endTime, controlLineId};
		List<Map<String, Object>> bcsDataList = lineJdbcTemp.queryForList(sql, params);
		if (bcsDataList == null || bcsDataList.isEmpty()) {
			return null;
		}
		//	转换数据
		List<LabExpendOther> labExpendOtherList = new ArrayList<>();
		for (int i = 0, s = bcsDataList.size(); i < s; i++) {
			Map<String, Object> bcsData = bcsDataList.get(i);
			//	工控料仓名
			String bcsStorage = String.valueOf(bcsData.get("Storage"));
			//	工控材料名
			String bcsMaterial = String.valueOf(bcsData.get("Material"));
			//	下料时间
			Date datTim = DateUtil.convertDate(String.valueOf(bcsData.get("DatTim")));
			String taskTime = DateUtil.convertString(datTim);
			//	操作工
			String operator = String.valueOf(bcsData.get("Operator"));
			//	操作数量
			Double amount = Double.valueOf(String.valueOf(bcsData.get("Amount")));
			//	根据工控数据,查询erp的仓位
			//	erp的原料id
			Long erpMaterialId = null;
			//	工控机的原料id
			Long erpStorageId = null;
			SysStorageLog takeTimeOrStorage = sysStorageLogDao.findTakeTimeOrStorage(lineId, bcsMaterial, 1, taskTime);
			if (takeTimeOrStorage != null) {
				String erpMaterial = takeTimeOrStorage.getMaterial();
				erpMaterialId = takeTimeOrStorage.getMaterialId();
				erpStorageId = takeTimeOrStorage.getStorageId();
			}
			//	创建erp手动消耗数据
			LabExpendOther labExpendOther = new LabExpendOther();
			labExpendOther.setLineId(lineId);
			labExpendOther.setMaterialId(erpMaterialId);
			labExpendOther.setStorageId(erpStorageId);
			labExpendOther.setDosage(amount);
			labExpendOther.setStatus(StateEnum.ENABLED.getCode());
			labExpendOther.setRemark(operator);
			labExpendOther.setCreator(bcsCreater);
			labExpendOther.setExpendTime(datTim);
			labExpendOther.setCreateTime(new Date());
			labExpendOther.setControlMaterial(bcsMaterial);
			labExpendOther.setControlStorage(bcsStorage);
			labExpendOtherList.add(labExpendOther);
		}
		return labExpendOtherList;
	}
}
